package com.chaos.system.service;

import cn.hutool.core.util.ObjectUtil;
import com.chaos.system.dto.TConfigDto;
import com.chaos.framework.model.dto.ID;
import com.chaos.framework.model.dto.page.PageList;
import com.chaos.framework.model.dto.page.PageQueryDto;
import com.chaos.framework.model.entity.Table;
import com.chaos.framework.model.helper.ConvertHelper;
import com.chaos.framework.model.helper.PageHelper;
import com.chaos.system.entity.TConfig;
import com.chaos.system.data.TConfigData;
import com.chaos.system.mapper.TConfigMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author Jacky.Cui
 * @since 2024-11-13
 */
@Slf4j
@Component
public class TConfigService extends ServiceImpl<TConfigMapper, TConfig> {

    public ID insertModel(TConfigData data) {
        TConfig entity = ConvertHelper.convert(data, TConfig.class);
        return save(entity) ? new ID(entity.getId()) : null;
    }

    public boolean deleteModel(ID data) {
        return removeById(data.getId());
    }

    public boolean updateModelByID(TConfigData data) {
        TConfig entity = ConvertHelper.convert(data, TConfig.class);
        return update(entity, new UpdateWrapper<TConfig>().eq(Table.ID, data.getId()));
    }

    public TConfigData selectByID(ID data) {
        return ConvertHelper.convert(getOne(new QueryWrapper<TConfig>().eq(Table.ID, data.getId())), TConfigData.class);
    }

    public List<TConfigData> selectByData(TConfigDto data) {
        QueryWrapper<TConfig> query = new QueryWrapper<>();
        query.lambda()
                .eq(ObjectUtil.isNotEmpty(data.getConfigKey()), TConfig::getConfigKey, data.getConfigKey())
                .eq(ObjectUtil.isNotEmpty(data.getType()), TConfig::getType, data.getType())
                .eq(ObjectUtil.isNotEmpty(data.getStatus()), TConfig::getStatus, data.getStatus())
                .in(ObjectUtil.isNotEmpty(data.getInKeys()), TConfig::getConfigKey, data.getInKeys());
        query.orderByDesc(Table.ID);
        return ConvertHelper.convert(list(query), TConfigData.class);
    }

    public PageList<TConfigData> selectByPage(PageQueryDto<TConfigData> pageData) {
        QueryWrapper<TConfig> query = new QueryWrapper<>();
        query.orderByDesc(Table.ID);
        return PageHelper.pageList(page(PageHelper.page(pageData), query), TConfigData.class);
    }

}
